﻿using DatabaseUserTest;
using DatabaseUserTest.Controller;
using DatabaseUserTest.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WcfService.WCFModels;

namespace WcfService
{
    public class StandardService : IStandardService
    {
        private DestinationCTRL destCtrl = new DestinationCTRL();
        private DijkstraCTRL dijkCtrl = new DijkstraCTRL();
        private RouteCTRL routeCtrl = new RouteCTRL();
        private PlaneCTRL planeCtrl = new PlaneCTRL();
        private UserCTRL userCtrl = new UserCTRL();
        private OrderCTRL orderCtrl = new OrderCTRL();

        #region Destination Methods
        public List<WCFDestination> FindAllDestinations()
        {
            List<Destination> destList = destCtrl.FindAllDestinations();
            List<WCFDestination> wcfDestList = new List<WCFDestination>();
            foreach (Destination item in destList)
            {
                wcfDestList.Add(ConvertDestinationToWCFDestination(item));
            }
            return wcfDestList;

        }

        public WCFDestination FindDestinationName(string name)
        {
            return ConvertDestinationToWCFDestination(destCtrl.FindDestinationName(name));
        }

        public List<WCFDestination> FindDestinationCountry(string country)
        {
            List<Destination> destList = destCtrl.FindDestinationCountry(country);
            List<WCFDestination> wcfDestList = new List<WCFDestination>();
            foreach (Destination item in destList)
            {
                wcfDestList.Add(ConvertDestinationToWCFDestination(item));
            }
            return wcfDestList;
        }
        #endregion

        public WCFCompleteRoute CalculateShortestPath(string from, string to, int check, DateTime date, int seats, bool firstClass)
        {
            return ConvertCompleteRouteToWCFCompleteRoute(dijkCtrl.CalculateShortestPath(from, to, check, date, seats, firstClass));
        }

        public WCFRoute FindRoute(string from, string to)
        {
            if (routeCtrl.FindRoute(from, to) == null)
            {
                return null;
            }
            else
            {
                return ConvertRouteToWCFRoute(routeCtrl.FindRoute(from, to));
            }
        }

        public WCFPlane FindPlane(DateTime departure, int routeId)
        {
            if (planeCtrl.FindPlane(departure, routeId) == null)
            {
                return null;
            }
            else
            {
                return ConvertPlaneToWCFPlane(planeCtrl.FindPlane(departure, routeId));
            }
        }

        public void ReserveSeats(int check, List<WCFPlane> planeList, int seats)
        {
            List<Plane> yesList = new List<Plane>();
            foreach (WCFPlane item in planeList)
            {
                yesList.Add(ConvertWCFPlaneToPlane(item));
            }

            planeCtrl.ReserveSeats(check, yesList, seats);

        }

        public void UnreserveSeats(int check, List<WCFPlane> planeList, int seats)
        {
            List<Plane> yesList = new List<Plane>();
            foreach (WCFPlane item in planeList)
            {
                yesList.Add(ConvertWCFPlaneToPlane(item));
            }

            planeCtrl.UnreserveSeats(check, yesList, seats);

        }

        public bool createUserAndOrder(string name, string phoneNo, string mail, double totalPrice, string route)
        {
            if(userCtrl.AddUser(name, phoneNo, mail) && orderCtrl.AddOrder(Convert.ToInt32(totalPrice), phoneNo, route))
            {               
                return true;
            }
            else
            {
                return false;
            }
        }

        #region Convert methods

        private WCFDestination ConvertDestinationToWCFDestination(Destination destination)
        {
            WCFDestination wcfDestination = new WCFDestination();
            wcfDestination.DestinationId = destination.DestinationId;
            wcfDestination.Name = destination.Name;
            wcfDestination.Country = destination.Country;
            return wcfDestination;

        }

        private Destination ConvertWCFDestinationToDestination(WCFDestination wcfDestination)
        {
            Destination destination = new Destination();
            destination.DestinationId = wcfDestination.DestinationId;
            destination.Name = wcfDestination.Name;
            destination.Country = wcfDestination.Country;
            return destination;
        }

        private Plane ConvertWCFPlaneToPlane(WCFPlane wcfPlane)
        {
            Plane plane = new Plane();
            plane.PlaneId = wcfPlane.PlaneId;
            plane.Gate = wcfPlane.Gate;
            plane.Departure = wcfPlane.Departure;
            plane.Arrival = wcfPlane.Arrival;
            plane.InteriorId = wcfPlane.InteriorId;
            plane.ReservedFirstClassSeats = wcfPlane.ReservedFirstClassSeats;
            plane.ReservedNormalSeats = wcfPlane.ReservedNormalSeats;
            plane.RouteId = wcfPlane.RouteId;
            return plane;
        }

        private WCFPlane ConvertPlaneToWCFPlane(Plane plane)
        {
            WCFPlane wcfPlane = new WCFPlane();
            wcfPlane.PlaneId = plane.PlaneId;
            wcfPlane.Gate = plane.Gate;
            wcfPlane.Departure = plane.Departure;
            wcfPlane.Arrival = plane.Arrival;
            wcfPlane.InteriorId = plane.InteriorId;
            wcfPlane.ReservedFirstClassSeats = plane.ReservedFirstClassSeats;
            wcfPlane.ReservedNormalSeats = plane.ReservedNormalSeats;
            wcfPlane.RouteId = plane.RouteId;
            return wcfPlane;
        }

        private WCFPlaneInterior ConvertPlaneInteriorToWCFPlaneInterior(PlaneInterior planeInterior)
        {
            WCFPlaneInterior wcfPlaneInterior = new WCFPlaneInterior();
            wcfPlaneInterior.InteriorId = planeInterior.InteriorId;
            wcfPlaneInterior.PlaneModel = planeInterior.PlaneModel;
            wcfPlaneInterior.TotalNormalSeats = planeInterior.TotalNormalSeats;
            wcfPlaneInterior.TotalFirstClassSeats = planeInterior.TotalFirstClassSeats;
            return wcfPlaneInterior;

        }

        private PlaneInterior ConvertWCFPlaneInteriorToPlaneInterior(WCFPlaneInterior wcfPlaneInterior)
        {
            PlaneInterior planeInterior = new PlaneInterior();
            planeInterior.InteriorId = wcfPlaneInterior.InteriorId;
            planeInterior.TotalNormalSeats = wcfPlaneInterior.TotalNormalSeats;
            planeInterior.TotalFirstClassSeats = wcfPlaneInterior.TotalFirstClassSeats;
            return planeInterior;
        }

        private WCFRoute ConvertRouteToWCFRoute(Route route)
        {
            WCFRoute wcfRoute = new WCFRoute();
            wcfRoute.RouteId = route.RouteId;
            wcfRoute.FromDestination = route.FromDestination;
            wcfRoute.ToDestination = route.ToDestination;
            wcfRoute.Time = route.Time;
            wcfRoute.Price = route.Price;
            return wcfRoute;
        }

        private Route ConvertWCFRouteToRoute(WCFRoute wcfRoute)
        {
            Route route = new Route();
            route.RouteId = wcfRoute.RouteId;
            route.FromDestination = wcfRoute.FromDestination;
            route.ToDestination = wcfRoute.ToDestination;
            route.Time = wcfRoute.Time;
            route.Price = wcfRoute.Price;
            return route;
        }

        private WCFCompleteRoute ConvertCompleteRouteToWCFCompleteRoute(CompleteRoute completeRoute)
        {
            WCFCompleteRoute wcfCompleteRoute = new WCFCompleteRoute();
            wcfCompleteRoute.BestAllDestinations = completeRoute.BestAllDestinations;
            wcfCompleteRoute.BestTotalCost = completeRoute.BestTotalCost;
            wcfCompleteRoute.SecondAllDestinations = completeRoute.SecondAllDestinations;
            wcfCompleteRoute.SecondTotalCost = completeRoute.SecondTotalCost;
            wcfCompleteRoute.ThirdAllDestinations = completeRoute.ThirdAllDestinations;
            wcfCompleteRoute.ThirdTotalCost = completeRoute.ThirdTotalCost;
            return wcfCompleteRoute;
        }

        private CompleteRoute ConvertWCFCompleteRouteToCompleteRoute(WCFCompleteRoute wcfCompleteRoute)
        {
            CompleteRoute completeRoute = new CompleteRoute();
            completeRoute.BestAllDestinations = wcfCompleteRoute.BestAllDestinations;
            completeRoute.BestTotalCost = wcfCompleteRoute.BestTotalCost;
            completeRoute.SecondAllDestinations = wcfCompleteRoute.SecondAllDestinations;
            completeRoute.SecondTotalCost = wcfCompleteRoute.SecondTotalCost;
            completeRoute.ThirdAllDestinations = wcfCompleteRoute.ThirdAllDestinations;
            completeRoute.ThirdTotalCost = wcfCompleteRoute.ThirdTotalCost;
            return completeRoute;
        }

        private User ConvertWCFUserToUser(WCFUser wcfUser)
        {
            User user = new User();
            user.Name = wcfUser.Name;
            user.PhoneNo = wcfUser.PhoneNo;
            user.Mail = wcfUser.Mail;
            return user;
        }

        #endregion
    }
}
